package boot.spring.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.Session;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import boot.spring.service.WebSocketServer;

@Controller
public class ChatController {
    
    @RequestMapping("/onlineusers")
    @ResponseBody
    public List<String> getOnlineUsers(@RequestParam("currentuser") String currentUser) {
        ConcurrentHashMap<String, Session> sessionPools = WebSocketServer.getSessionPools();
        List<String> onlineUsers = new ArrayList<>();
        
        for (String username : sessionPools.keySet()) {
            if (!username.equals(currentUser)) {
                onlineUsers.add(username);
            }
        }
        
        return onlineUsers;
    }
}
